var app = getApp()

Page({
  data: {
    cid: null,
    countSign: null,
    deleteCan: 'none',
    details: null,
    displayTable: '',
    display0: 'none'
  },
  onReady: function () {
    if (app.globalData.userNum == 2) {
      wx.setNavigationBarColor({
        frontColor: '#000000',
        backgroundColor: '#99CCCC',
        animation: {
          duration: 550
        }
      })
    }
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({ cid: options.cid })
    this.setData({ countSign: options.countSign })
  },
  onShow: function () {
    var that = this
    var countSign = that.data.countSign
    that.studentDetail()
    if (app.globalData.tid != -1 && app.globalData.tid != null && app.globalData.tid != 0){
      that.setData({ deleteCan: ''})
      if(countSign==0){
        that.setData({ deleteCan: 'none' })
        that.setData({ display0: '' })
      }
    }
  },
  studentDetail: function(){
    var that = this
    var CId = that.data.cid
    var countSign = that.data.countSign
    var url = app.globalData.basePath + "StuFind/studentsDetail.do";
    wx.request({
      url: url,
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      data: {
        'CId': CId,
        'countSign': countSign
      },
      success: function (result) {
        result = result.data
        console.log(result)
        if (countSign == 0){
          that.setData({ displayTable: 'none' })
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '还没有进行签到过',
            success: function (res) {
              console.log("mmp")
            }
          })
        }else{
          var i = 0
          for (i in result) {
            switch (result[i]["signstate"]) {
              case 1: result[i]["signstate"] = "请假"; break;
              case 3: result[i]["signstate"] = "迟到"; break;
              case 2:
              case 4: result[i]["signstate"] = "早退"; break;
              case 5: result[i]["signstate"] = "正常"; break;
              default: result[i]["signstate"] = "未到"; break;
            }
          }
          that.setData({ details: result })
        }
      }
    })
  },
  backToTabBar: function () {
    wx.switchTab({
      url: '../classes/classes',
    })
  },
  deleteRecent: function(){
    var that = this
    var CId = that.data.cid
    var countSign = that.data.countSign
    var url = app.globalData.basePath + "SCFind/deleteRecent.do";

    wx.showModal({
      title: '提示',
      content: '确认删除吗？删除后本次签到的数据无法恢复！',
      success: function (res) {
        if (res.confirm) {
          console.log('点击了确认删除按钮')
          wx.request({
            url: url,
            method: 'POST',
            header: {
              'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            },
            data: {
              'CId': CId,
              'SignCount': countSign
            },
            success: function (result) {
              result = result.data
              console.log(result)
              if (result["status"] == 1) {
                countSign = countSign - 1

                var url1 = app.globalData.basePath + "find/updateClassByWx.do";
                wx.request({
                  url: url1,
                  method: 'POST',
                  header: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                  },
                  data: {
                    'CId': CId,
                    'SignCount': countSign
                  },
                  success: function (result) {
                  }
                })

                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '成功删除最近的一次签到情况',
                  success: function (res) {
                    console.log("ojbk")
                    that.setData({ cid: CId })
                    that.setData({ countSign: countSign })
                    that.studentDetail()
                    if (app.globalData.tid != -1 && app.globalData.tid != null && app.globalData.tid != 0) {
                      that.setData({ deleteCan: '' })
                    }
                  }
                })

              }else{
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '删除失败！请重新尝试或者重新登录系统尝试。(可联系faceSchool公众号进行反馈)',
                  success: function (res) {
                    console.log("mmp")
                  }
                })
              }

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  }
})